package server.service.appointment;

import java.util.List;

import org.json.JSONArray;
import org.json.JSONObject;

import domain.appointment.Appointment;
import domain.session.Response;

public class AppointmentAction {
	
	private AppointmentModel apptModel;
	
	public AppointmentAction() {
		apptModel = new AppointmentModel();
	}
	
	public Response addAppointment(final JSONObject requestJson) {
		Appointment appt = new Appointment(requestJson.getJSONObject("appointment"));
		apptModel.addAppointment(appt);
		Response response = new Response();
		response.setSuccessful(true);
		return response;
	}
	
	public Response removeAppointment(final JSONObject requestJson) {
		Appointment rappt = new Appointment(requestJson.getJSONObject("appointment"));
		apptModel.remove(rappt);
		Response response = new Response();
		response.setSuccessful(true);
		return response;
	}

	public Response getPatientAppointments(final JSONObject requestJson) {
		Integer pid = requestJson.getInt("userId");
		List<Appointment> pAppts = apptModel.getPatientAppointments(pid);
		JSONArray pApptArray = new JSONArray();
		
		for (Appointment a : pAppts) {
			pApptArray.put(a.getJSON());
		}
		JSONObject responseJson = new JSONObject();
		responseJson.put("appointments", pApptArray);
		Response response = new Response(true, responseJson);
		return response;
	}

	public Response getDoctorAppointments(final JSONObject requestJson) {
		Integer did = requestJson.getInt("userId");
		List<Appointment> dAppts = apptModel.getDoctorAppointments(did);
		JSONArray dApptArray = new JSONArray();
		
		for (Appointment a : dAppts) {
			dApptArray.put(a.getJSON());
		}
		JSONObject responseJson = new JSONObject();
		responseJson.put("appointments", dApptArray);
		Response response = new Response(true, responseJson);
		return response;
	}

	public Response getLastMonthDoctorAppointments(final JSONObject requestJson) {
		Integer docid = requestJson.getInt("userId");
		List<Appointment> docAppts = apptModel.getLastMonthDoctorAppointments(docid);
		JSONArray docApptArray = new JSONArray();
		
		for (Appointment a : docAppts) {
			docApptArray.put(a.getJSON());
		}
		JSONObject responseJson = new JSONObject();
		responseJson.put("appointments", docApptArray);
		Response response = new Response(true, responseJson);
		return response;
	}

	public Response getFutureDoctorAppointments(final JSONObject requestJson) {
		Integer fDocid = requestJson.getInt("userId");
		List<Appointment> fDocAppts = apptModel.getFutureDoctorAppointments(fDocid);
		JSONArray fDocApptArray = new JSONArray();
		
		for (Appointment a : fDocAppts) {
			fDocApptArray.put(a.getJSON());
		}
		JSONObject responseJson = new JSONObject();
		responseJson.put("appointments", fDocApptArray);
		Response response = new Response(true, responseJson);
		return response;
	}

	public Response getFuturePatientAppointments(final JSONObject requestJson) {
		Integer fPid = requestJson.getInt("userId");
		List<Appointment> fPAppts = apptModel.getFuturePatientAppointments(fPid);
		JSONArray fPApptArray = new JSONArray();
		
		for (Appointment a : fPAppts) {
			fPApptArray.put(a.getJSON());
		}
		JSONObject responseJson = new JSONObject();
		responseJson.put("appointments", fPApptArray);
		Response response = new Response(true, responseJson);
		return response;
	}

	public Response saveAppointment(final JSONObject requestJson) {
		apptModel.save();
		Response response = new Response();
		response.setSuccessful(true);
		return response;
	}

}
